﻿using AutoMapper;
using B.S.BasicData.Api.Write.Applications.Command;
using B.S.BasicData.Domains;
using B.S.BasicData.Domains.Dto;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructures;
using MediatR;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.EntityFrameworkCore.Metadata;

namespace B.S.BasicData.Api.Write.Applications.CommandHandler
{
    public class AddRoleCommandHandler : IRequestHandler<AddRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Role> roleRepository;
        private readonly IBaseRepository<RolePermissons> rolepermissonRepository;
        private readonly IBaseRepository<Permissons> permissonRepository;
        private readonly IMapper mapper;
        private readonly ILogger<AddRoleCommandHandler> logger;


        public AddRoleCommandHandler(IBaseRepository<Role> roleRepository, IBaseRepository<RolePermissons> rolepermissonRepository, IBaseRepository<Permissons> permissonRepository, IMapper mapper, ILogger<AddRoleCommandHandler> logger)
        {
            this.roleRepository = roleRepository;
            this.rolepermissonRepository = rolepermissonRepository;
            this.permissonRepository = permissonRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        public async Task<ApiResult<int>> Handle(AddRoleCommand request, CancellationToken cancellationToken)
        {
            
            
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.Success;
            result.Message = "角色权限添加成功";
            using (var tran=roleRepository.DB.Database.BeginTransaction())
            {
                try
                {
                    //角色
                    logger.LogInformation("角色权限添加成功");
                    Role role = mapper.Map<Role>(request);
                    role.CreateTime = DateTime.Now;
                    role.State = false;
                    await roleRepository.Add(role);
                    //权限角色
                    var list = new List<RolePermissons>();
                    foreach(var item in request.PermissonIds)
                    {
                        list.Add(new RolePermissons

                        {
                            RoleId = role.RoleId,
                            PermissonId = item


                        }

                            );
                            
                    }
                      
                    
                    await rolepermissonRepository.AddRange(list);
                    
                    tran.Commit();
                }
                catch(Exception ex)
                {
                    logger.LogError("角色权限添加失败"+ex.Message);
                    result.Code = ApiEnum.Fail;
                    result.Message = "角色权限添加失败";
                    tran.Rollback();
                }
            }
                
            return await Task.FromResult(result);



        }
    }
}
